<template>
	<view class="doctorExpress">
		<searchBox :currentCode='currentCode' @handleSearch='handleSearch'></searchBox>
		<view class="content" v-if="expressList.length>0">
			<expressItem v-for="(item, index) in expressList" :key="index" :orderObj='item' :itemIndex='index'
				:btnGroupArr="btnGroupArr" @handleClick="consultationTableButtonClick"></expressItem>
			<view class="loadmoreView">
				<u-loadmore :status="status" />
			</view>

		</view>
		<pagesNoData v-else></pagesNoData>
	</view>
</template>

<script>
	import {
		serviceStatusMap,
		ningboOrderTypeMap,
		ordersStatusMap
	} from '@/utils/baseData.js'
	import expressItem from '@/components/common/expressItem.vue'
	import {
		mapState
	} from 'vuex'
	import {
		getExpressAgencyAllCourier
	} from '@/api/platform/expressInfo.js'
	import {
		getDicts
	} from '@/api/platform/dicts.js'
	import {
		getInstitutionSearch
	} from '@/api/platform/institutions.js'
	export default {
		name: "doctorExpress",
		components: {
			expressItem,
		},
		props: {
			currentCode: {
				type: String,
				default: ''
			},
		},
		data() {
			return {
				serviceStatusMap: serviceStatusMap,
				ningboOrderTypeMap: ningboOrderTypeMap,
				ordersStatusMap: ordersStatusMap,
				expressList: [],
				btnGroupArr: [{
						label: '签收',
						functionKey: 'receive'
					},

					{
						label: '回寄',
						functionKey: 'back'
					},
					{
						label: '查看',
						functionKey: 'watch'
					}

				],
				status: 'loadmore',
				total: 0,
				filterForm: {
					name: '', //患者姓名
					consignee: '', //收件人
					consignor: '', //寄件人
					expressNumber: '', //快递单号
					orderId: null, //订单id
					serialNumber: null, //订单号
					// receiveStatus: 2, //收件状态
					receiveStatus: '', //收件状态
					sendingType: '', //0, //寄送类别
					signer: '', //签收人
					sendingCourierId: '', //寄送快递机构id
					receivingCourierId: '', //收件快递机构id
					sendStartTime: '', //寄送开始时间
					sendEndTime: '', //寄送结束时间
					page: 0,
					size: 10
				},
				allinstitutionList: [],
				selectInstitutionList: [],
				currentinstitutionId: this.$store.state.user.institution.id,
				sexKindList: [],
			};
		},
		mounted() {
			this.getSexList()
		},
		methods: {
			handleSearch(data) {
				console.log('handleSearch---data',data)
				this.expressList=[]
				this.filterForm=data
				this.getInstitutionList()
			},
			consultationTableButtonClick(functionKey, row) { // 总的事件方法
				if (functionKey == 'receive') {
					this[functionKey](row, 2)
				} else if (functionKey == 'back') {
					this[functionKey](row, 3)
				} else if (functionKey == 'watch') {
					this[functionKey](row, 4)
				}
			},
			//签收
			receive(row, userStatus) {
				let expressId = row.id
				let orderId = row.orderId
				let expressuserStatus = userStatus

				uni.navigateTo({
					url: `/subPackages/appointment/uploadDelivery/uploadDelivery?expressuserStatus=${expressuserStatus}&orderId=${orderId}&expressId=${expressId}`
				})
			},
			//回寄
			back(row, userStatus) {
				let expressId = row.id
				let orderId = row.orderId
				let expressuserStatus = userStatus
				uni.navigateTo({
					url: `/subPackages/appointment/uploadDelivery/uploadDelivery?expressuserStatus=${expressuserStatus}&orderId=${orderId}&expressId=${expressId}`
				})
			},
			//查看
			watch(row, userStatus) {
				let expressId = row.id
				let orderId = row.orderId
				let expressuserStatus = userStatus
				uni.navigateTo({
					url: `/subPackages/appointment/uploadDelivery/uploadDelivery?expressuserStatus=${expressuserStatus}&orderId=${orderId}&expressId=${expressId}`
				})
			},
			//获取性别
			getHospitalBySEX(id) {
				let obj = this.sexKindList.find(item => item.id == id)
				if (obj) {
					return obj.name
				}
			},
			//获取医院信息
			getHospitalById(id) {
				let obj = this.allinstitutionList.find(item => item.id == id)
				if (obj) {
					return obj.name
				}

			},
			getSexList() {
				let query = {
					code: 'SexKind'
				}
				getDicts(query).then(res => {
					if (res.code == 200) {
						this.sexKindList = res.data
						this.getInstitutionList()
					}
				})
			},
			getInstitutionList() {
				let query = {
					page: 0,
					size: 999,
				}
				getInstitutionSearch(query).then(res => {
					if (res.code == 200) {
						let data = res.data
						this.allinstitutionList = data
						this.selectInstitutionList = []
						if (this.currentinstitutionId && this.currentinstitutionId != 1) {
							data.forEach(t => {
								if (t.id == this.currentinstitutionId) {
									this.selectInstitutionList.push({
										id: t.id,
										name: t.receivingCourier + '(' + t.name + ')',
										receiveAddress: t.receiveAddress,
										consignee: t.consignee,
										consigneePhoneNumber: t.consigneePhoneNumber,
										receivingCourier: t.receivingCourier
									})
								}

							})
							this.btnGroupArr = [{
									label: '签收',
									functionKey: 'receive'
								},

								{
									label: '回寄',
									functionKey: 'back'
								},

								{
									label: '查看',
									functionKey: 'watch'
								}
							]
						} else {
							this.consultationTable.operation = [{
								label: '查看',
								functionKey: 'watch'
							}]
							this.selectInstitutionList = data
						}
						this.diagnosisTableRequest()

					}
				})
			},
			scrolltolower() {
				let that = this;
				this.status = 'loading';
				this.filterForm.page = ++this.filterForm.page;
				setTimeout(() => {
					if (this.expressList.length < this.total) {
						this.status = 'loadmore'
						that.diagnosisTableRequest()
					} else {
						this.status = 'nomore'
					}

				}, 600)
			},
			diagnosisTableRequest() { //整理调度
				getExpressAgencyAllCourier(this.filterForm).then(res => {
					if (res.code == 200) {
						this.total = res.pageInfo.total
						let resContent = res.data
						let expressList = []

						if (res.data&&resContent.length > 0) {
							expressList = resContent.map((t, index) => ({
								index,
								id: t.id,
								orderId: t.orderId,
								serialNumber: t.serialNumber,
								// orderNumber: t.expressInfoGroupNumber,
								expressContent: JSON.parse(t.expressContent),
								consignorPhoneNumber: JSON.parse(t.expressContent)
									.consignorPhoneNumber,
								expressName: JSON.parse(t.expressContent).expressName,
								expressNumber: JSON.parse(t.expressContent).expressNumber,
								sendTime: t.sendTime,
								serviceStatusId: t.serviceStatus,
								serviceStatus: serviceStatusMap[t.serviceStatus],
								ordersStatus: ordersStatusMap[t.ordersStatus],
								ordersStatusId: t.ordersStatus,
								receiveStatusTxt: t.receiveStatus == 1 && t.sendingType == 1 ?
									'已签收' : t.receiveStatus == 1 && t.sendingType == 0 ?
									'已签收,待回寄' : t.receiveStatus == 2 ?
									'未签收' : t.receiveStatus == 3 ?
									'已回寄' : t.receiveStatus == 4 ?
									'已退回' : '异常',

								receiveStatus: t.receiveStatus,
								receiveTime: t.signTime,
								backTime: t.backTime,
								signer: t.signer,
								remark: JSON.parse(t.expressContent).remark,
								sendingType: t.sendingType == 0 ? '寄送' : t.sendingType == 1 ? '回寄' :
									'',
								// inspectionMethod: this.getInspectionMethodName(t.inspectionMethod),
								sendingCourier: this.getHospitalById(t
									.sendingCourierId
									), //(t.sendingCourier ? t.sendingCourier : '') + '(' + this.getHospitalById(t.sendingCourierId) + ') ',
								consignor: t.consignor,
								consignee: t.consignee,
								receivingCourierId: t.receivingCourierId ? t.receivingCourierId : '',
								sendingCourierId: t.sendingCourierId ? t.sendingCourierId : '',
								receivingCourier: this.getHospitalById(t
									.receivingCourierId
									), //(t.receivingCourier ? t.receivingCourier : '') + '(' + this.getHospitalById(t.receivingCourierId) + ')',
								sexName: this.getHospitalBySEX(t.sex),
								name: t.name,
								age: t.age,
								applyTime: t.applyTime,
								sendNumber: t.sendNumber,
							}))
							// console.log('expressList', expressList)
						}
						this.expressList.push(...expressList)
						if (this.expressList.length < this.total) {
							this.status = 'loadmore'
						} else {
							this.status = 'nomore'
						}
					}
				})

			},

		}
	}
</script>

<style lang="scss" scoped>
	.doctorExpress {
		height: 100%;

		&_content {
			height: 100%;
		}
	}
</style>